<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    </head>
    <body>
        <div class="container">
            <h2>基本使用</h2>
            <button class="btn btn-primary">发送get请求</button>
            <button class="btn btn-warning">发送post请求</button>
            <button class="btn btn-success">发送put请求</button>
            <button class="btn btn-danger">发送delete请求</button>
        </div>
        <script>
            // http://123.207.32.32:8000/home/multidata
            // http://123.207.32.32:8000/home/data?type=pop&page=2
            console.log(axios);
            // 获取按钮
            const btn = document.querySelectorAll("button");
            btn[0].onclick = function(){
                axios({
                    method:"get",
                    url:"http://127.0.0.1:3000/posts/2"
                }).then(response => {
                    console.log(response);
                })
            }
            //添加一篇新的文章
            btn[1].onclick = function(){
                //发送ajax请求
                axios({
                    //请求类型
                    method:"post",
                    // url
                    url:"http://127.0.0.1:3000/posts",
                    // 设置请求体
                    data:{
                        title:"呀呀呀呀呀",
                        author:"啦啦啦啦"
                    }
                }).then(response => {
                    console.log(response);
                })
            }
            // 更新数据
            btn[2].onclick = function(){
                //发送ajax请求
                axios({
                    //请求类型
                    method:"put",
                    // url
                    url:"http://127.0.0.1:3000/posts/3",
                    // 设置请求体
                    data:{
                        title:"呀呀呀呀呀",
                        author:"heiheieheiheie"
                    }
                }).then(response => {
                    console.log(response);
                })
            }
            // 删除数据
            btn[3].onclick = function(){
                //发送ajax请求
                axios({
                    //请求类型
                    method:"delete",
                    // url
                    url:"http://127.0.0.1:3000/posts/3"
                }).then(response => {
                    console.log(response);
                })
            }

            // 处理并发请求的助手函数 axios.all
            /* axios.all([axios({
                url:"http://123.207.32.32:8000/home/multidata"
            }),axios({
                url:"https://api.apiopen.top/getJoke?page=1&count=10&type=text"
            })]).then(response =>{
                console.log(response);// 返回的  数组
            }) */
            // axios.all 将数据展开
            axios.all([axios({
                url:"http://123.207.32.32:8000/home/multidata"
            }),axios({
                url:"https://api.apiopen.top/getJoke?page=1&count=10&type=text"
            })]).then(axios.spread((res1,res2) =>{
                // 数据展开
                console.log(res1);
                console.log(res2);
            }));


            /*
                支持多种请求方式 
                axios(config)
                axios.request(config)
                axios.get(url[, config])
                axios.delete(url[, config])
                axios.head(url[, config])
                axios.options(url[, config])
                axios.post(url[, data[, config]])
                axios.put(url[, data[, config]])
                axios.patch(url[, data[, config]])
            */
        </script>
    </body>
</html>